;(function(){
  'use strict';
  angular.module('services.omdisplay.upayQM')
    .factory('TradeStatRepFormServer',tradeStatRepFormServer)
    .factory('TradeStatRepFormSelecotor',tradeStatRepFormSelecotor)
    .factory('TradeStatRepFormQueryParam',tradeStatRepFormQueryParam)
    .factory('TradeStatRepFormViewObj',tradeStatRepFormViewObj);
  function tradeStatRepFormServer(Restangular){
    return{
      tradeStatRepFormQuery : tradeStatRepFormQuery
    };
    
    function tradeStatRepFormQuery(param,callback){
      return Restangular.all('sas/OMdisplay/upayQM/businessForm/tradeStatistics').post(param).then(callback);
    }
  }

  tradeStatRepFormServer.$inject = ['Restangular'];

  function tradeStatRepFormSelecotor(COREDATA,UpayQMServer){
    var selector = {
      provinces : COREDATA.PROVINCES,
      banks : UpayQMServer.getBank(),
      tradeType : [{name:'缴费',code:'01'},{name:'充值',code:'02'},{name:'冲正',code:'03'},{name:'退费',code:'04'},{name:'扣款',code:'05'}],
      tradeStat : [{name:'成功',code:'01'},{name:'失败',code:'02'}],
      tradeInitiator : [{name:'移动',code:'01'},{name:'银行',code:'02'}],
      numberIdentifying : [{name:'主号',code:'01'},{name:'副号',code:'02'},{name:'非签约号码',code:'03'}]
    };
    return {
      selector : selector
    };
  }

  tradeStatRepFormSelecotor.$inject = ['COREDATA','UpayQMServer'];

  function tradeStatRepFormQueryParam(UpayQMServer,CoreService,TradeStatRepFormSelecotor,TradeStatRepFormViewObj,TradeStatRepFormServer){
    var param = {};
    var date = {};
    var flag = {};

    return{
      initFlag : initFlag,
      initDate : initDate,
      initParam : initParam,
      tradeStatRepFormQueryFn : tradeStatRepFormQueryFn
    };

    function initFlag(){
      flag = {
        formIsFold : false,
        isQuery : false
      };
      return flag;
    }

    function initParam(){
      param = {
        quarter:{}
      };
      return param;
    }

    function initDate(){
      if(is.not.undefined(param.timeType)){
        date = UpayQMServer.initDate(param.timeType.timeTypeCode);
      }else{
        date = UpayQMServer.initDate('1');
      }
      return date;
    }
    
    function tradeStatRepFormQueryFn(){
      var tempParam = {};
      if(!flag.formIsFold){
        tempParam = setParam();
        TradeStatRepFormServer.tradeStatRepFormQuery(tempParam,function(res){
          if(res.state !== '10000' || checkData(res.data)){
            CoreService.dialog(res.state);
            return;
          }
          TradeStatRepFormViewObj.setSeries(res.data,param,TradeStatRepFormSelecotor.selector);
          TradeStatRepFormViewObj.setExportParam(tempParam);
          flag.formIsFold = !flag.formIsFold;
          flag.isQuery = true;
        });
      }else{
        flag.formIsFold =! flag.formIsFold;
      }
    }

    function setParam(){
      var tempParam = {};
      var formatArr = [
        undefined,'yyyy-MM-dd',undefined,'yyyy-MM-01','yyyy-01-01','yyyy-01-01'
      ];
      var format;
      if(param.timeType){
        tempParam.timeType = param.timeType.timeTypeCode;
      }else{
        tempParam.timeType = '1';
      }
      format = formatArr[tempParam.timeType];
      if(param.timeType){
        switch(param.timeType.timeTypeCode){
          case '4':
            tempParam.startTime = param.quarter.startTime.substr(0,4)+'-01-01';
            tempParam.endTime = param.quarter.endTime.substr(0,4)+'-01-01';
            tempParam.startQuarter = param.quarter.startTime.substr(4,1);
            tempParam.endQuarter = param.quarter.endTime.substr(4,1);
            break;
          case '5':
            tempParam.startTime = date.startTime+'-01-01';
            tempParam.endTime = date.endTime+'-01-01';
            break;
          default :
            tempParam.startTime = CoreService.dateFormat(date.startTime,format);
            tempParam.endTime = CoreService.dateFormat(date.endTime,format);
        }
      }else{
        tempParam.startTime = CoreService.dateFormat(date.startTime,format);
        tempParam.endTime = CoreService.dateFormat(date.endTime,format);
      }
      return tempParam;
    }

    function checkData(data){
      var isNull = true;
      if(data && is.array(data) && data.length !== 0){
        isNull = false;
      }
      return isNull;
    }
  }

  tradeStatRepFormQueryParam.$inject = ['UpayQMServer','CoreService','TradeStatRepFormSelecotor','TradeStatRepFormViewObj','TradeStatRepFormServer'];

  function tradeStatRepFormViewObj(){
    var viewObj = {};
    return {
      initViewObj : initViewObj,
      setSeries : setSeries,
      setExportParam : setExportParam
    };

    function initViewObj(){
      viewObj = {
        series : [],
        exportParam :{}
      };
      return viewObj;
    }

    function setSeries(data,param,selector){
      var series = {};
      // var selected = {};
      // if(param.provinceCode){
      //   selected.provinceCode = param.provinceCode;
      // }else{
      //   selected.provinceCode = selector.provinces;
      // }
      // if(param.bankCode){
      //   selected.bankCode = param.bankCode;
      // }else{
      //   selected.bankCode = selector.banks;
      // }
      // if(param.numberIdentifying){
      //   selected.numberIdentifying = param.numberIdentifying;
      // }else{
      //   selected.numberIdentifying = selector.numberIdentifying;
      // }
      // angular.forEach(selected.provinceCode,function(item){
      //   series[item.provinceCode] = {
      //     provinceName:item.provinceName,
      //     data:{}
      //   };
      //   angular.forEach(selected.bankCode,function(subItem){
      //     series[item.provinceCode].data[subItem.agencyCode] = {
      //       bankName:item.agencyName,
      //       data:{}
      //     };
      //     angular.forEach(selected.numberIdentifying,function(grandSonItem){
      //       series[item.provinceCode].data[subItem.agencyCode].data[grandSonItem.name] = {
      //         name:grandSonItem.name,
      //         data:{}
      //       };
      //     });
      //     series[item.provinceCode].data[subItem.agencyCode].data['total'] = {
      //       name:'小计'
      //     };
      //   })
      //   series[item.provinceCode].data['total'] = {
      //     bankName:'合计'
      //   };
      // });
      // series.total = {
      //   provinceName:'总计'
      // };
      // angular.forEach(data,function(item){
      //   series[item.provinceCode].data[item.bankCode].bankName = item.bankName;
      //   series[item.provinceCode].data[item.bankCode].data[item.numberIdentifying].data = item;
      // });
      viewObj.series = data;
      return viewObj.series;
    }

    function setExportParam(param){
      viewObj.exportParam = param;
      return viewObj.exportParam;
    }
  }

})();